package {
	import com.googlecode.milib.display.service.MLButton;	
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLActionClass;
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEventInfo;	
	import com.googlecode.milib.display.button.AbstractButton;	
	
	/**
	 * @author Marek Brun 'minim'
	 */
	public class MCActionController extends MLBaseClass implements IMLBroadcasterListener {

		private var mlmc:MLMC;
		private var action:MLActionClass;
		private var mc_circle : MovieClip;
		private var mc_flag : MovieClip;
		private var btn:MLButton;

		public function MCActionController(mc:MovieClip, action:MLActionClass, name:String) {
			mlmc=MLMC.forInstance(mc);
			this.action=action;
			action.addListener(this);
			TextField(mc.getChildByName('tf')).text=name;
			mc_circle=MovieClip(mc.getChildByName('circle'));
			mc_flag=MovieClip(mc.getChildByName('flag'));
			
			btn=MLButton.forInstance(mc);
			btn.addListener(this);
			
			draw();
		}//<>
		
		protected function draw():void {
			if(action.getIsRunning()){
				mc_circle.gotoAndPlay(1);
			}else{
				mc_circle.gotoAndStop(mc_circle.totalFrames);
			}
			mc_flag.gotoAndStop(action.getIsRunningFlag() ? 1 : 2);
		}//<<
		
		public function get mc():MovieClip { return mlmc.mc; };
		
//********************************************************************************************
//		events for MCActionController
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case action:
					switch(ev.event) {
						case action.event_ActionStart:
							draw();
						break;
						case action.event_ActionFinish:
							draw();
						break;
						case action.event_RunningFlagChanged:
							draw();
						break;
					}
				break;
				case btn:
					switch(ev.event) {
						case btn.event_Press:
							action.setIsRunning(!action.getIsRunning());
						break;
					}
				break;
			}
		}//<<
		
	}
}
